package com.zyf.connotation.presenter

import android.util.Log
import com.zyf.connotation.bean.Connotation
import com.zyf.connotation.model.ConnotationModel
import com.zyf.connotation.model.IConnotationModel
import com.zyf.connotation.utils.RequestMap
import com.zyf.connotation.view_interface.IConnotationView
import com.zyf.rxjavaapp.utils.ServiceUtils
import com.zyf.rxjavaapp.webinterface.WebService
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.*

/**
 * 作者：Think
 * 创建于 2017/5/13 08:38
 */
class ConnotationPresenter{
    private var iConnotationView:IConnotationView? =null
    private var connotationModel: IConnotationModel? = null
    var local_request_tag:String =""
    var am_loc_time:String =""
    var min_time:String =""
    var service = ServiceUtils.createService(WebService::class.java)
    constructor(iConnotationView: IConnotationView){
        this.iConnotationView = iConnotationView
        connotationModel = ConnotationModel()
    }
    /**
     * 请求服务器
     * */
    fun <T> getDataList(t:T,istop:Boolean){
        local_request_tag = "" + Date().time
        am_loc_time = "" + (Date().time - 10000)
        min_time = "" + (Date().time - 72000)
        val call = service.getList(local_request_tag, am_loc_time, min_time.substring(0, min_time.length - 3),"-102")
        connotationModel?.netDatat(t,call)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe ({
            iConnotationView?.DataList(it,istop)
        },{iConnotationView?.error("网络错误")})
    }
    /**
     * 请求服务器
     * */
    fun <T> getVideoList(t:T,istop:Boolean){
        local_request_tag = "" + Date().time
        am_loc_time = "" + (Date().time - 10000)
        min_time = "" + (Date().time - 72000)
        val call = service.getList(local_request_tag, am_loc_time, min_time.substring(0, min_time.length - 3),"-104")
        connotationModel?.netDatat(t,call)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe ({
            iConnotationView?.DataList(it,istop)
        },{
            iConnotationView?.error("网络错误")
        })
    }
    /**
     * 点赞或者踩
     * */
    fun netFabulous(position:Int,check:String,data: Connotation.Data){
        connotationModel?.netFabulous(check,data.group.group_id)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe ({
            iConnotationView?.fabulous(position,check)
            if(check.equals("digg")){
                data.group.user_digg = 1
                data.group.digg_count +=1
            }else{
                data.group.user_bury =1
                data.group.bury_count +=1
            }
        },{
            iConnotationView?.error("网络错误")
        })
    }
    /**
     * 获取评论
     * */
    fun <T> getComments(t:T,id:String,offset:Int){
        val call = service.getComments(id,id,offset)
        connotationModel?.netDatat(t,call)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe ({
            iConnotationView?.DataList(it,false)
        },{
            iConnotationView?.error("网络错误")
        })
    }
    /**
     * 获取评论
     * */
    fun <T> getDiggUser(t:T,id:String){
        connotationModel?.DiggUser(t,id)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe({
            iConnotationView?.DataList(it,true)
        },{
            iConnotationView?.error("网络错误")
        })
    }
    /**
     * 发表评论  评论数组传入 评论成功之后 把返回的评论信息加入评论数组   调接口刷新评论列表适配器 ,comments:ArrayList<Comments.RecentComments>
     * */
    fun <T> sendComment(t:T,id:String,text:String){
        var request = RequestMap.getMap()
        request.put("action","comment")
        request.put("group_id",id)
        request.put("item_id",id)
        request.put("forum_id","0")
        request.put("text",text)
        request.put("is_comment","1")
        val call = service.sendComment(request)
        connotationModel?.netDatat(t,call)!!
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe ({
                iConnotationView?.DataList(it,false)
        },{
            iConnotationView?.error("网络错误")
        })
    }
}